/**
* 
*/
package com.java.controller.admin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.java.entity.Good;
import com.java.entity.Order;
import com.java.entity.Payment;
import com.java.entity.Returns;
import com.java.entity.User;
import com.java.service.impl.GoodServiceImpl;
import com.java.service.impl.OrderServiceImpl;
import com.java.service.impl.PaymentServiceImpl;
import com.java.service.impl.ReturnServiceImpl;
import com.java.service.impl.UserServiceImpl;

/**
* @author Kidney
* 创建时间：2019年4月29日 下午3:04:51
* Description:
*/
/**
 * @author KIDNEY
 *
 */
@Controller
@RequestMapping("/returns/")
public class RerurnController {

	@Autowired
	private OrderServiceImpl os;

	@Autowired
	private ReturnServiceImpl rs;

	@Autowired
	private PaymentServiceImpl ps;

	@Autowired
	private UserServiceImpl us;

	@RequestMapping(value = "all", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String allReturns() {

		List<Returns> listReturns = rs.all();

		String jsonString = JSON.toJSONString(listReturns);
		return jsonString;
	}

	@RequestMapping(value = "pass", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Integer pass(Integer id) {

		Returns r = rs.getReturn(id);
		System.out.println(r);
		r.setPass(true);

		return rs.pass(r);
	}

	@RequestMapping(value = "insert", produces = "application/json; charset=utf-8")
	@ResponseBody
	public ModelAndView insert(Integer orderid, String expressid) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/pages/front/return.jsp");
		Returns r = new Returns();
		r.setOrderid(orderid);
		r.setExpressid(expressid);
		int flag = rs.insert(r);
		if (flag == 1) {
			mav.getModel().put("status", "申请成功!!");
		} else {
			mav.getModel().put("status", "申请失败!!");
		}

		return mav;
	}

	@RequestMapping(value = "userreturn", produces = "application/json; charset=utf-8")
	@ResponseBody
	public ModelAndView user(HttpServletRequest request) {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("/pages/front/myreturn.jsp");

		Cookie[] cookies = request.getCookies();

		String username = null;

		for (Cookie cookie : cookies) {
			if (cookie.getName().equals("username")) {
				username = cookie.getValue();
			}
		}

		User selectedUser = us.selectByName(username);
		System.out.println(selectedUser);
		int selectedId = selectedUser.getUserId();

		List<Returns> returns = rs.userReturn(selectedId);
		System.out.println(returns);
		mav.getModel().put("list", returns);

		return mav;

//		JSONArray array = new JSONArray();
//
//		List<Map<String, Object>> alist = new ArrayList<Map<String, Object>>();
//		for (int i = 0; i < returns.size(); i++) {
//			JSONObject jsonObj = new JSONObject();
//			HashMap<String, Object> map = new HashMap<String, Object>();
//
//			map.put("returnid", returns.get(i).getOrderid());
//
//			int orderid = returns.get(i).getOrderid();
//			Order order = os.selectByPrimary(orderid);
//			int paymentid = order.getPaymentId();
//			Payment payment = ps.selectByPrimaryKey(paymentid);
//			String name = payment.getName();
//
//			map.put("orderid", orderid);
//			map.put("paymentid", paymentid);
//			map.put("name", name);
//			map.put("pass", returns.get(i).getPass());
//
//			array.add(map);
//		}
//		return array.toJSONString();

	}

}
